﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using NUnit.Framework;
using CosyTrade.Distribution;
using CosyTrade;

namespace NUnit.Test
{
    [TestFixture]
    public class Class1
    {
        [TestFixtureSetUp]
        public void init()
        {
            string defaultDbConfigPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "dbConfig.xml");
            XmlConfigurationSource source = new XmlConfigurationSource(defaultDbConfigPath);
            Type[] ormTypes = GetOrmTypes();
            
            ActiveRecordStarter.Initialize(source, ormTypes);
        }

        private static Type[] GetOrmTypes()
        {
            Assembly contractAssembly =
                Assembly.LoadFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CosyTrade.Contract.dll"));
            Type[] assemblyTypes = contractAssembly.GetTypes();
            List<Type> ormTypes = new List<Type>();
            foreach (Type assemblyType in assemblyTypes)
            {
                if (!assemblyType.IsDefined(typeof(ActiveRecordAttribute), false)) continue;
                if (!assemblyType.IsDefined(typeof(SerializableAttribute), false))
                    throw new Exception(string.Format("{0} Not Mark SerializableAttribute", assemblyType.FullName));
                ormTypes.Add(Type.GetType(assemblyType.AssemblyQualifiedName));
            }
            return ormTypes.ToArray();
        }

        [Test]
        public void GetCategorySubList()
        {
            IList<ClientCategory> categories = new ClientRelationManager().GetCategorySubList(new Guid("d42887e4-df96-4c41-86cc-e3885d1d4780"));
            if(categories==null)
                Console.WriteLine("没有了记录");
            categories[0].CaptionChinese = "新分类";
            new ClientRelationManager().UpdateClientCategory(categories[0]);
        }

        [Test]
        public void GetRegionSubList()
        {
            Region region = new Region();
            //region.
            //IList<Region> regions = new ClientRelationManager().GetRegionSubList("")
        }
    }
}
